import {Component, OnInit} from '@angular/core';
import {Nav} from '../../models/nav';
import {NavService} from '../../services/nav/nav.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.less']
})
export class HeaderComponent implements OnInit {

  today: Date = new Date();

  user: {
    username?: string;
  } = {};

  openNavs: Array<Nav>;

  constructor(private navSvr: NavService, private router: Router) {
  }

  ngOnInit() {
    this.user = {
      username: sessionStorage.getItem('username')
    };

    /*初始换上方导航栏*/
    this.navSvr.getAllNav().subscribe(data => {
      this.openNavs = data;
      this.navSvr.openNavs = data;
    });

  }

  changeTab(nav: Nav,i: number): void {
    for (const item of this.openNavs) {
      item.isActive = false;
    }
    nav.isActive = true;
    /*调转是调节菜单栏变化*/
    this.navSvr.index= i;
    this.router.navigateByUrl(nav.link);
  }

  logout(): void {
    sessionStorage.removeItem('username');
    this.router.navigateByUrl('/login');
  }


}
